package com.useeinfo.oa.modules.base.biz;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.func.lang.StringUtils;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.enums.ResultEnums;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.dao.PersonnnelOrganizationDao;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.entity.Facility;
import com.useeinfo.oa.modules.base.entity.PersonnnelOrganization;
import com.useeinfo.oa.modules.security.biz.SystemUserBiz;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @program: YiYouFuPlatform
 * @author: Taogq
 * @createDate: 2018-12-04
 * @createTime: 16:12:33
 */
@Service
public class PersonnnelOrganizationBiz extends CrudBiz<PersonnnelOrganizationDao, PersonnnelOrganization> {

    @Autowired
    private HttpServletRequest httpServletRequest;
    @Autowired
    private EnterpriseBiz enterpriseBiz;
    @Autowired
    private SystemUserBiz systemUserBiz;
    @Autowired
    private FacilityBiz facilityBiz;

    public List<PersonnnelOrganization> hqlExcute(String hql){
        return dao.hqlExcute(hql);
    }

    public ResultDto orderInfo(Integer pageIndex, Integer pageSize){
        JSONObject result = new JSONObject();
        SystemUser systemUser = SessionKeyUtils.getSessionUser(httpServletRequest);
        Enterprise sessionEnterprise = SessionKeyUtils.getSessionEnterprise(httpServletRequest);
        if (systemUser == null || sessionEnterprise == null) {
           return ResultUtil.error(ResultEnums.NO_LOGIN_MESSAGE);
        }
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("systemUserId", systemUser.getId().toString());
        List<Long> ids = new ArrayList<>();
        String idArray = StringUtils.strip(ids.toString(),"[]");
        List<Enterprise> enterprises = enterpriseBiz.findList(queryParam);
        for(Enterprise enterprise : enterprises){
            ids.add(enterprise.getId());
        }
        QueryParam userParam = new QueryParam();
        userParam.getSqlMap().put("enterpriseIds", idArray);
        List<SystemUser> users = systemUserBiz.findList(userParam);
        List<Object[]> orderInfos = dao.orderInfo(pageIndex, pageSize, ids);
        QueryParam facilityParam = new QueryParam();
        facilityParam.getSqlMap().put("enterpriseIds", idArray);
        List<Facility> facilities = facilityBiz.findList(facilityParam);
        JSONArray jsonArray = new JSONArray();
        for (Object[] orderInfo : orderInfos) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("moduleCode",StringConverters.null2String(orderInfo[0]));
            jsonObject.put("moduleName",StringConverters.null2String(orderInfo[1]));
            jsonObject.put("orderId",StringConverters.null2String(orderInfo[2]));
            jsonObject.put("createDate",DateTimeUtils.format(DateTimeUtils.parse("yyyy-MM-dd",StringConverters.null2String(orderInfo[3])),"yyyy-MM-dd"));
            jsonObject.put("orderCode",StringConverters.null2String(orderInfo[4]));
            jsonObject.put("userName",StringConverters.null2String(orderInfo[5]));
            jsonObject.put("receiveDate",orderInfo[6] == null ? "" : DateTimeUtils.format(DateTimeUtils.parse("yyyy-MM-dd",StringConverters.null2String(orderInfo[6])),"yyyy-MM-dd"));
            jsonObject.put("presenceTime",orderInfo[7] == null ? "" : DateTimeUtils.format(DateTimeUtils.parse("yyyy-MM-dd",StringConverters.null2String(orderInfo[7])),"yyyy-MM-dd"));
            jsonObject.put("finishTime",orderInfo[8] == null ? "" : DateTimeUtils.format(DateTimeUtils.parse("yyyy-MM-dd",StringConverters.null2String(orderInfo[8])),"yyyy-MM-dd"));
            jsonObject.put("content",StringConverters.null2String(orderInfo[9]));
            jsonObject.put("color",StringConverters.null2String(orderInfo[10]));
            jsonObject.put("areaName",StringConverters.null2String(orderInfo[11]));
            jsonObject.put("majorName",StringConverters.null2String(orderInfo[12]));
            jsonObject.put("address",StringConverters.null2String(orderInfo[13]));
            jsonArray.add(jsonObject);
        }
        result.put("orderInfoNum",dao.orderInfoSize(ids));
        result.put("enterpriseNum",ids.size());
        result.put("userNum",users.size());
        result.put("facilityNum",facilities.size());
        result.put("rows",jsonArray);
        return ResultUtil.success(result);
    }




    public ResultDto orderNum(String dayNum){
        Enterprise sessionEnterprise = SessionKeyUtils.getSessionEnterprise(httpServletRequest);
        String enterpriseId = StringConverters.null2String(sessionEnterprise.getId());
        JSONArray result = new JSONArray();
        int intDayNum = StringConverters.null2Int(dayNum) - 1 ;
        int absDayNum =  intDayNum - (intDayNum + intDayNum);
        Date date=new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, absDayNum);
        date = calendar.getTime();
        List<String> dates = DateTimeUtils.getDates(date,new Date(), "yyyy-MM-dd");
        for(String dateTime : dates){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("date",dateTime);
            jsonObject.put("num",dao.getOrderNumByDate(dateTime,"MAINTAIN",enterpriseId) + dao.getOrderNumByDate(dateTime,"REPAIR",enterpriseId) + dao.getOrderNumByDate(dateTime,"INSPECTION",enterpriseId));
            result.add(jsonObject);
        }
        return ResultUtil.success(result);
    }

}
